<template>
  <div>
    <van-list
      class="cmt-list"
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <van-cell v-for="item in list" :key="item.com_id">
        <template>
          <div class="box">
            <van-image round fit="cover" :src="item.aut_photo" />
            <div class="info-box">
              <p class="name">{{ item.aut_name }}</p>
              <div class="content">{{ item.content }}</div>
              <div>
                <span class="time">{{ item.pubdate | relvTime }}</span>
                <van-button @click="doReply(item)" class="replay-btn" size="mini"
                  >回复({{ item.reply_count }})</van-button
                >
              </div>
            </div>
            <van-icon name="like-o" />
          </div>
        </template>
      </van-cell>
    </van-list>

    <!-- 发表评论区域 -->
    <div class="send-box">
      <van-field
        v-model.trim="msg"
        center
        clearable
        placeholder="请输入评论内容"
        @keyup.enter="send"
      >
        <template #button>
          <van-button @click="send" size="small" type="info">发布</van-button>
        </template>
      </van-field>
    </div>

    <!-- sheet区域--弹出回复 -->
    <!-- v-model控制显示和隐藏，这里的隐藏仅仅只是样式的隐藏，所以数据还在 -->
    <!-- 我希望的是每次隐藏，这个reply组件都会销毁，那样下次再显示reply就会重新创建 -->
    <van-action-sheet v-model="replyShow" title="回复列表">
      <reply v-if="replyShow" :currentItem="current"/>
    </van-action-sheet>
  </div>
</template>

<script>
import { cmtListAPI, sendCmtAPI } from "@/api";
import reply from './reply.vue'
export default {
  components: {
    reply
  },
  data() {
    return {
      list: [],
      loading: false,
      finished: false,
      offset: null,
      msg: '',
      replyShow: false ,
      current: {}
    };
  },
  methods: {
    async onLoad() {
      let res = await cmtListAPI({
        type: "a",
        source: this.$route.query.id,
        offset: this.offset,
      });

      this.list.push(...res.data.data.results);
      // 把本次返回的最后一条赋值给offset，方便下次从这个数据后面开始查后面的数据
      this.offset = res.data.data.last_id;
      // 本次加载完毕
      this.loading = false;
      // 判断是否全部加载完
      if (res.data.data.end_id == this.offset) {
        this.finished = true;
      }
    },

    async send () {

        // 非空判断
        if (this.msg == '') return this.$toast('请输入评论内容')

        // 证明不为空，就可以发请求
        let res = await sendCmtAPI({
            target: this.$route.query.id,
            content: this.msg
        })
        
        // 为了避免刷新，直接把返回的评论数据插入到数组中即可
        this.list.unshift(res.data.data.new_obj)

        // 把输入内容清空
        this.msg = ''
        this.$toast.success('评论成功')
    },

    doReply (item) {

      // 需要显示出回复面板
      this.replyShow = true

      // 需要把当前的item传递给回复面板
      this.current = item 
    }
  },
};
</script>

<style lang="less" scoped>
.box {
  display: flex;

  .van-image {
    width: 40px;
    height: 40px;
    margin-right: 15px;
  }

  .info-box {
    flex: 1;
    .name {
      color: #466b9d;
    }
    .time {
      color: #ccc;
      margin-right: 10px;
    }
    .van-button {
      vertical-align: middle;
    }
  }
}

.send-box {

    position: fixed;
    // 定位后是没有宽度的都是靠内容撑开
    width: 100%;
    bottom: 0;
    left: 0;
}

.cmt-list {
    margin-bottom: 53px;
}
</style>